import { defineStore } from "pinia";
import { ref } from "vue";
import { type LoadingInstance } from "element-plus/lib/components/loading/src/loading.js";
import { ElLoading } from "element-plus";

const KEY = "loading";

/**
 * 全局加载
 */
export const useLoadingStore = defineStore(KEY, () => {
  const instance = ref({} as LoadingInstance);

  function loading(text?: string) {
    instance.value = ElLoading.service({
      fullscreen: true,
      text: text || "加载中,请稍后...",
    });
  }

  function done() {
    instance.value.close();
  }

  return {
    loading,
    done,
  };
});
